#include "./res/m.h"


// 时间更新定时器
void update_time(lv_timer_t * timer) 
{
    static char buf[50]; // 存储时间字符串，格式为"HH:MM:SS"
    time_t now;
    time(&now);
    struct tm * time_info = localtime(&now);
    int year = time_info->tm_year + 1900;
    int month = time_info->tm_mon + 1;
    int day = time_info->tm_mday;
    int hour = time_info->tm_hour;
    int minute = time_info->tm_min;
    int second = time_info->tm_sec;

    // 格式化时间字符串
    snprintf(buf, sizeof(buf), "%04d/%02d/%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
    
    // 更新标签文本
    lv_label_set_text(timer->user_data, buf);
}



//时间显示模块
void time_display(lv_obj_t *win)
{
    // 创建一个按钮
    lv_obj_t *btn = lv_btn_create(win);
    lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 40, 20);
    lv_obj_set_size(btn, 350, 50);

    // 创建一个样式
    static lv_style_t style;
    lv_style_init(&style);

    // 设置按钮背景的透明度
    lv_style_set_bg_opa(&style, LV_OPA_60);
    // 设置按钮背景的颜色
    lv_style_set_bg_color(&style, lv_palette_main(LV_PALETTE_GREEN));
    // 将样式应用到按钮上
    lv_obj_add_style(btn, &style, LV_PART_MAIN);
    
    // 用定时器来显示时间
    lv_obj_t * label_time = lv_label_create(btn);
    // 时间格式
    lv_label_set_text(label_time, "0000/00/00 00:00:00");
    lv_obj_set_style_text_font(label_time, &lv_font_montserrat_32, 0);
    lv_obj_align(label_time, LV_ALIGN_CENTER, 0, 0);

    // 创建定时器，每0.5秒更新一次时间
    lv_timer_create(update_time, 500, label_time);

    return;
}
